/**
 * Created by XC on 2017/07/20.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');
var async = require( 'async'); // 同步控制


// Schema 结构
var ItbDatumTmplModel = function(obj){
    ItbBaseModel.call(this,obj);

  // 以下为表定义数据，需要的字段就填入
  //`dt_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '资料模版id',
  //`type` int(2) NOT NULL DEFAULT '0' COMMENT '0无, 10用户资料, 20公司资料',
  //`data` mediumtext COMMENT '模版内容（json字符串）: SJGTGV3结构',
  //`ver` int(10) NOT NULL DEFAULT '0' COMMENT '提纲结构版本号',
  //`upd_cnt` int(10) NOT NULL DEFAULT '0' COMMENT '版本号内模版提纲更新次数',
  //`desc` varchar(100) DEFAULT NULL COMMENT '备注',
  //`publish` int(1) NOT NULL DEFAULT '0' COMMENT '0制作中; 1已完成',
  //`create_time` datetime NOT NULL,
  //`update_time` datetime NOT NULL,
  //`update_by` varchar(50) NOT NULL DEFAULT '',
  //`del_flg` int(1) NOT NULL DEFAULT '0',

};
ItbDatumTmplModel.prototype = new ItbBaseModel();

// 取得表名
ItbDatumTmplModel.prototype.getTableName = function(){
    return 'itb_datum_tmpl';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
ItbDatumTmplModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'dt_id='+this.dt_id;
};

// 查找执行类型的资料模版所有有效字体
ItbDatumTmplModel.prototype.getInfoByType = function(type, callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbDatumTmplModel.getInfoByType Err:'+err);
        }
        // Use the connection
        var sql = 'SELECT t.* '+
            ' FROM itb_datum_tmpl t '+
            ' WHERE t.del_flg = 0 '+
            ' AND t.publish=1 '+
            ' AND t.type='+type+
            ' ORDER BY t.ver DESC, t.upd_cnt DESC ';
        //var sqlValue = [];
        //var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();

            console.log();

            callback(error, result);
        });
    });
};

// 导出
module.exports = ItbDatumTmplModel;